"""
Case Type   : 功能测试
Case Name   : 资源池化支持外表
Create At   : 2023-12-11
Owner       : kyeleze
Description :
    1.创建wrapper
    2.创建server
    3.创建foreign table
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common

class PageTableTestCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.primary_node = Node('PrimaryDbUser')
        self.wrapper_name = 'fdw_page_table_test054'
        self.server_name = 'ser_page_table_test054'
        self.table_name = 't_page_table_test054'
        self.defalt_support_extended_features = \
            self.common.show_param('support_extended_features')

    def test_page_table(self):
        step = "----step1:创建wrapper expect：成功----"
        self.log.info(step)
        self.log.info('----设置support_extended_features= on----')
        guc_res = self.primary_sh.execute_gsguc('set',
                                                self.constant.GSGUC_SUCCESS_MSG,
                                                f'support_extended_features= on')
        self.assertTrue(guc_res, '执行失败:' + step)
        self.log.info('----重启数据库----')
        restart_cmd = f'source {macro.DB_ENV_PATH};' \
                      f'cm_ctl stop && cm_ctl start'
        restart_res = self.common.get_sh_result(self.primary_node, restart_cmd)
        self.log.info(restart_res)
        self.assertIn(self.constant.cm_stop_success_msg, restart_res,
                      '执行失败:' + step)
        self.assertIn(self.constant.cm_start_success_msg, restart_res,
                      '执行失败:' + step)
        self.log.info('----查询support_extended_features参数值----')
        show_res = self.common.show_param('support_extended_features')
        self.assertIn('on', show_res, '执行失败:' + step)
        sql_cmd = f'''
            drop foreign data wrapper if exists {self.wrapper_name};
            create foreign data wrapper {self.wrapper_name};'''
        sql_res = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn("CREATE FOREIGN DATA WRAPPER", sql_res,
                      '执行失败:' + step)

        step = "----step2:创建server expect：成功----"
        self.log.info(step)
        sql_cmd = f'''
            drop server if exists {self.server_name};
            create server {self.server_name} foreign data wrapper 
            {self.wrapper_name};'''
        sql_res = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn("CREATE SERVER", sql_res, '执行失败:' + step)
        
        step = "----step3:创建foreign table expect：成功----"
        self.log.info(step)
        sql_cmd = f'''
            drop foreign table if exists {self.table_name};
            create foreign table {self.table_name} (a int, b int, c text) 
            server {self.server_name};'''
        sql_res = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, sql_res, 
                      '执行失败:' + step)

    def tearDown(self):
        step = "----step4:清理环境 expect:成功----"
        self.log.info(step)
        sql_cmd = f'''
            drop foreign table if exists {self.table_name};
            drop server if exists {self.server_name};
            drop foreign data wrapper if exists {self.wrapper_name};'''
        sql_res = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.log.info('----恢复默认值----')
        self.common.show_param('support_extended_features')
        guc_res = self.primary_sh.execute_gsguc(
            'set',self.constant.GSGUC_SUCCESS_MSG,
            f'support_extended_features= '
            f'{self.defalt_support_extended_features}')
        self.log.info('----查询support_extended_features参数值----')
        self.log.info('----重启数据库----')
        restart_cmd = f'source {macro.DB_ENV_PATH};' \
                      f'cm_ctl stop && cm_ctl start'
        restart_res = self.common.get_sh_result(self.primary_node, restart_cmd)
        self.log.info(restart_res)
        self.assertIn(self.constant.cm_stop_success_msg, restart_res,
                      '执行失败:' + step)
        self.assertIn(self.constant.cm_start_success_msg, restart_res,
                      '执行失败:' + step)
        show_res = self.common.show_param('support_extended_features')
        self.assertIn(self.defalt_support_extended_features, show_res,
                      '执行失败:' + step)
        self.log.info(f'----{os.path.basename(__file__)} end----')
